//
//  FANRetweetView.m
//  Weibo - OC
//
//  Created by user on 16/4/21.
//  Copyright © 2016年 user. All rights reserved.
//

#import "FANRetweetView.h"
#import "FANPictureCollectionView.h"
#import <YYText/YYText.h>
//#warning 宏定义一定要放到 导入头文件的 上面, 会影响编译

//define this constant if you want to use Masonry without the 'mas_' prefix

#define MAS_SHORTHAND

//define this constant if you want to enable auto-boxing for default syntax

#define MAS_SHORTHAND_GLOBALS

#import <Masonry/Masonry.h>

#define kMargin 10


@interface FANRetweetView ()

@property (nonatomic, strong) YYLabel *retweetLabel;

@property (nonatomic, strong) FANPictureCollectionView *pictureView;

@property (nonatomic, strong) MASConstraint *bottomCons;

@end

@implementation FANRetweetView

- (FANPictureCollectionView *)pictureView{
    
    if (!_pictureView) {
        
        _pictureView = [[FANPictureCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];

    }
    
    return _pictureView;
}

- (YYLabel *)retweetLabel{
    
    if (!_retweetLabel) {
        
        _retweetLabel = [[YYLabel alloc] init];
        
        _retweetLabel.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 2 * kMargin;
        
        _retweetLabel.font = [UIFont systemFontOfSize:15];
        
        _retweetLabel.numberOfLines = 0;
        
        _retweetLabel.textColor = [UIColor darkGrayColor];
        
        _retweetLabel.userInteractionEnabled = true;
    }
    
    return _retweetLabel;
}

- (void)setStatusViewModel:(FANStatusViewModel *)statusViewModel{
    
    _statusViewModel = statusViewModel;
    
//    self.retweetLabel.text = statusViewModel.retweetText;
    
    self.retweetLabel.attributedText = statusViewModel.retweetAttributString;
    
    [self.bottomCons uninstall];
    
    if (self.statusViewModel.status.retweeted_status.pic_urls && statusViewModel.status.retweeted_status.pic_urls.count > 0) {
        
        if (statusViewModel.status.retweeted_status.pic_urls.count == 1) {
            
            UICollectionViewFlowLayout *layout =(UICollectionViewFlowLayout *)self.pictureView.collectionViewLayout;
            
            layout.itemSize = statusViewModel.retweetPictureSize;
            
        }else{
            
            UICollectionViewFlowLayout *layout =(UICollectionViewFlowLayout *)self.pictureView.collectionViewLayout;
            
            layout.itemSize = CGSizeMake(FANPictureWH, FANPictureWH);
        }
        
        [self.pictureView updateConstraints:^(MASConstraintMaker *make) {
           
            make.size.equalTo(self.statusViewModel.retweetPictureSize);
            
        }];
        
        self.pictureView.hidden = false;
        
        self.pictureView.pic_urls = statusViewModel.status.retweeted_status.pic_urls;
        
        [self makeConstraints:^(MASConstraintMaker *make) {
           
            self.bottomCons = make.bottom.equalTo(self.pictureView.bottom).offset(FANMargin);
            
        }];
        
    }else{
        
        self.pictureView.hidden = true;
        
        [self makeConstraints:^(MASConstraintMaker *make) {
           
            self.bottomCons = make.bottom.equalTo(self.retweetLabel.bottom).offset(FANMargin);
            
        }];
        
    }
    
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    
    self.backgroundColor = [UIColor colorWithWhite:245/255.0 alpha:1];
    
    [self addSubview:self.retweetLabel];
    [self addSubview:self.pictureView];
    
    [self.retweetLabel makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.leading.equalTo(self).offset(kMargin);
        
        make.trailing.equalTo(self).offset(-kMargin);
    }];
    
    [self.pictureView makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.retweetLabel.bottom).offset(FANMargin);
        
        make.leading.equalTo(self.retweetLabel.leading);
        
        make.width.height.equalTo(100);
        
    }];
    
    [self makeConstraints:^(MASConstraintMaker *make) {
       
       self.bottomCons = make.bottom.equalTo(self.pictureView.bottom).offset(kMargin);
        
    }];
}

@end
